import React, { useEffect, useState } from 'react';
import { Button, Input, Layout } from '@arco-design/web-react';
import '@arco-design/web-react/dist/css/arco.css';
import Title from '@arco-design/web-react/es/Typography/title';
import { upApkInfoView } from './UpApkInfoView';
import { BuildResult, GitResult } from "../../main/constants/GlobalConfig";

const { TextArea } = Input;
const { Header, Content, Footer } = Layout;

function HomeScreen() {
  const [androidProjectPath, setAndroidProjectPath] = useState<string>(
    'D:\\TradeGo\\buildApk\\TradeGo_APP_APK',
  );
  const [shuoming, setShuoming] = useState<string>('');
  const [buildResult, setBuildResult] = useState<string>('编译日志');
  const [gitResult, setGitResult] = useState<string>('');
  const [uploadResult, setUploadResult] = useState<any>(null);

  const buildApk = (isGoogle: boolean) => {
    console.log(window.electron); // 检查是否可用
    const params = {
      androidProjectPath,
      shuoming,
    };
    setBuildResult('正在打包...');
    setUploadResult(null);
    setGitResult("");
    if (isGoogle) {
      window.electron.ipcRenderer.sendMessage('build-apk-google', params);
    } else {
      window.electron.ipcRenderer.sendMessage('build-apk', params);
    }
  };

  useEffect(() => {
    const updateDes = localStorage.getItem('updateDes');
    setShuoming(updateDes);
    if (window.electron) {
      window.electron.ipcRenderer.on(BuildResult, (message: string) => {
        console.log('Log from build:', message);
        setBuildResult(message);
      });
      window.electron.ipcRenderer.on('apk-result', (message: any) => {
        console.log('Log from apk-result:', buildResult);
        setUploadResult(message);
      });
      window.electron.ipcRenderer.on(GitResult, (message: any) => {
        console.log('Log from git-result:', buildResult);
        setGitResult((prevGitResult) => prevGitResult + '' + message);
      });
      return () => {
        window.electron.ipcRenderer.on(BuildResult, () => {});
      };
    }
  }, []);

  return (
    <div className="layout-basic-demo">
      <Layout>
        <Title style={{ alignSelf: 'center' }}>打包apk上传到蒲公英</Title>
        <Content>
          <div style={{ padding: '20px' }}>
            <Input
              style={{ width: 400, minHeight: 40, paddingLeft: 10 }}
              placeholder="打包路径"
              value={androidProjectPath}
              onChange={(e) => setAndroidProjectPath(e)}
            />

            <Button
              style={{ marginLeft: 20 }}
              type="primary"
              onClick={() => buildApk(false)}
            >
              打包公版
            </Button>
            <Button
              style={{ marginLeft: 20 }}
              type="primary"
              onClick={() => buildApk(true)}
            >
              打包Google包
            </Button>
            <TextArea
              placeholder="输入更新说明："
              style={{ minHeight: 64, minWidth: 350, marginTop: 20 }}
              value={shuoming}
              onChange={(e) => {
                setShuoming(e);
                localStorage.setItem('updateDes', e);
              }}
            />
            {upApkInfoView(uploadResult)}
            <div style={{ marginTop: 30, whiteSpace: 'pre-line' }}>
              git更新日志 <br />
              {gitResult}
            </div>
            <div style={{ marginTop: 30, whiteSpace: 'pre-line' }}>
              打包结果 <br />
              {buildResult}
            </div>
          </div>
        </Content>
      </Layout>
    </div>
  );
}

export default HomeScreen;
